import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import { createPinia } from 'pinia'
import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/reset.css'
import { renderWithQiankun, qiankunWindow } from 'vite-plugin-qiankun/dist/helper'

import App from './App.vue'
import routes from './router'

let app: any = null

function render(props: any = {}) {
  const { container } = props
  
  const router = createRouter({
    history: createWebHistory(qiankunWindow.__POWERED_BY_QIANKUN__ ? '/master-data' : '/'),
    routes,
  })
  
  const pinia = createPinia()
  app = createApp(App)
  
  app.use(router)
  app.use(pinia)
  app.use(Antd)
  
  const containerElement = container ? container.querySelector('#master-data-app') : document.querySelector('#master-data-app')
  app.mount(containerElement || '#master-data-app')
}

renderWithQiankun({
  mount(props) {
    console.log('[master-data-app] props from main framework', props)
    render(props)
  },
  bootstrap() {
    console.log('[master-data-app] bootstraped')
  },
  unmount() {
    if (app) {
      app.unmount()
      app = null
    }
  },
})

// 独立运行时
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  render()
}

